<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of WeatherApi
 *
 * @author Administrator
 */
class WeatherApi {
    //put your code here
    public function __construct() {
        
    }
    //获得省份编码
    public function getProvinceInfo(){
        $url="http://www.weather.com.cn/data/city3jdata/china.html";
        $html=  Common::Curl_Get($url,"http://www.weather.com.cn/pubmodel/inquires2.htm");
        $json=  json_decode($html,TRUE);
        return $json;
  
    }
    public function getCityInfo($province_code){
            $url="http://www.weather.com.cn/data/city3jdata/provshi/{$province_code}.html";
            $html=  Common::Curl_Get($url,"http://www.weather.com.cn/pubmodel/inquires2.htm");
            $json=  json_decode($html,TRUE);
            return $json;
    }
    public function getAreaInfo($city_code){
        $url="http://www.weather.com.cn/data/city3jdata/station/{$city_code}.html";
        $html=  Common::Curl_Get($url,"http://www.weather.com.cn/pubmodel/inquires2.htm");
        $json=  json_decode($html,TRUE);
        return $json;
    }
    public function getCodeByCityName($city){
        if($city){
            $city=str_replace("市", "", $city);
            $url="http://toy1.weather.com.cn/search?cityname={$city}&callback=success_jsonpCallback&_=".  time();
            $html=Common::Curl_Get($url, "http://www.weather.com.cn/");
            $match=array();
            preg_match("/~?[0-9]{9}(?=~)/is", $html,$match);
            if(isset($match[0])){
                return trim($match[0]);
            }
            return "";
            
        }
    }
    //获得当前天气信息的实时部分3小时更新一次的
    public function getRelParty($code){
        $url="http://d1.weather.com.cn/sk_2d/{$code}.html?_=".time();
        $html=  Common::Curl_Get($url,"http://www.weather.com.cn/weather1d/{$code}.shtml");
        $html=str_replace("var dataSK = ", "", $html);
        $json=  json_decode($html,TRUE);
        $data_rtime=array();
        if(!empty($json)){
            $data_rtime["nameen"]=$json["nameen"];
            $data_rtime["cityname"]=$json["cityname"];
            $data_rtime["weatherID"]=$json["city"];
            $data_rtime["temp"]=  floatval($json["temp"]);
            $data_rtime["tempf"]=floatval($json["tempf"]);
            $data_rtime["wd"]=$json["WD"];
            $data_rtime["wde"]=$json["wde"];
            $data_rtime["ws"]=$json["WS"];
            $data_rtime["wse"]=$json["wse"];
            $data_rtime["sd"]=$json["SD"];
            $rtime=strtotime(date("Y-m-d")." ".$json["time"].":00");
            $data_rtime["rtime"]= $rtime?$rtime:0;
            $data_rtime["weather"]=$json["weather"];
            $data_rtime["aqi"]=intval($json["aqi"]);
            if($data_rtime["aqi"]<=50){
                $data_rtime["aqiname"]="优";
            }elseif($data_rtime["aqi"]>50&&$data_rtime["aqi"]<=100){
                $data_rtime["aqiname"]="良";
            }elseif($data_rtime["aqi"]>100&&$data_rtime["aqi"]<=150){
                $data_rtime["aqiname"]="轻度污染";
            }elseif($data_rtime["aqi"]>150&&$data_rtime["aqi"]<=200){
                $data_rtime["aqiname"]="中度污染";
            }elseif($data_rtime["aqi"]>200&&$data_rtime["aqi"]<=300){
                $data_rtime["aqiname"]="重度污染";
            }elseif($data_rtime["aqi"]>300){
                $data_rtime["aqiname"]="严重污染";
            }
            return $data_rtime;
        }
        return NULL;
    }
    //获得当前天气信息的非实时部分，一天更新一次
    public function getNrelParty($code){
        //采白天和夜间信息
        $url="http://www.weather.com.cn/weather1d/{$code}.shtml";
        $html=  Common::Curl_Get($url,"http://www.weather.com.cn/");
        $match=array();
        $match2=array();
        $data_day=array();
        $html=  str_replace("\r\n", "", $html);
        $html=  str_replace("\r", "", $html);
        $html=  str_replace("\n", "", $html);
        preg_match("/<ul class=\"clearfix\"><li>.*?<\/div><\/li>/is", $html,$match);
        preg_match("/<div class=\"slid\"><\/div><\/li><li>.*?<\/li><\/ul>/is", $html,$match2);
        if(isset($match[0])){
            $day_wea=$this->getWea($match[0]);
            
            if($day_wea==FALSE){
                $day_wea=  $this->getTomorrowWea($match[0]);
            }
            $day_tem=$this->getTem($match[0]);
            
            $day_win=$this->getWin($match[0]);
            $day_sunup=$this->getSunUp($match[0]);
            $data_day["day_temp"]=$day_tem;
            $data_day["day_wea"]=$day_wea;
            $data_day["day_wd"]=isset($day_win["wd"])?$day_win["wd"]:"";
            $data_day["day_ws"]=isset($day_win["ws"])?$day_win["ws"]:"";
            if($day_sunup){
                $data_day["sun_up"]=strtotime(date("Y-m-d")." ".$day_sunup.":00");
            }
        }
        if(isset($match2[0])){
            $night_wea=$this->getWea($match2[0]);
            if($night_wea==FALSE){
                $night_wea=  $this->getTomorrowWea($match2[0]);
            }
            $night_tem=$this->getTem($match2[0]);
            $night_win=$this->getWin($match2[0]);
            $night_sundown=$this->getSunDown($match2[0]);
            $data_day["night_temp"]=$night_tem;
            $data_day["night_wea"]=$night_wea;
            $data_day["night_wd"]=isset($night_win["wd"])?$night_win["wd"]:"";
            $data_day["night_ws"]=isset($night_win["ws"])?$night_win["wd"]:"";
            if($night_sundown){
                $data_day["sun_down"]=strtotime(date("Y-m-d")." ".$night_sundown.":00");
            }
        }
        if(!empty($data_day)){
            return $data_day;
        }
        return NULL;
    }
    //获得当前的天气信息
    public function getTodayWeather($code){
        $url="http://d1.weather.com.cn/sk_2d/{$code}.html?_=".time();
        $html=  Common::Curl_Get($url,"http://www.weather.com.cn/weather1d/{$code}.shtml");
        $html=str_replace("var dataSK = ", "", $html);
        $json=  json_decode($html,TRUE);
        $data=array();
        $data_all=array();
        $data_day=array();
        $data_rtime=array();
        //采白天和夜间信息
        $url="http://www.weather.com.cn/weather1d/{$code}.shtml";
        $html=  Common::Curl_Get($url,"http://www.weather.com.cn/");
        $html=  str_replace("\r\n", "", $html);
        $html=  str_replace("\r", "", $html);
        $html=  str_replace("\n", "", $html);
        $match=array();
        $match2=array();
        preg_match("/<li class='dn on' data-dn='todayT'>.*?<\/li>/is", $html,$match);
        preg_match("/<li class='dn' data-dn='todayN'>.*?<\/li>/is", $html,$match2);
        if(isset($match[0])){
            $day_wea=$this->getWea($match[0]);
            if($day_wea==FALSE){
                $day_wea=  $this->getTomorrowWea($match[0]);
            }
            $day_tem=$this->getTem($match[0]);
            $day_win=$this->getWin($match[0]);
            $day_sunup=$this->getSunUp($match[0]);
            $data_day["day_temp"]=$data["day_temp"]=$day_tem;
            $data_day["day_wea"]=$data["day_wea"]=$day_wea;
            $data_day["day_wd"]=$data["day_wd"]=isset($day_win["wd"])?$day_win["wd"]:"";
            $data_day["day_ws"]=$data["day_ws"]=isset($day_win["ws"])?$day_win["ws"]:"";
            $data_day["sun_up"]=$data["sun_up"]=strtotime(date("Y-m-d")." ".$day_sunup.":00");
        }
        if(isset($match2[0])){
            $night_wea=$this->getWea($match2[0]);
            if($night_wea==FALSE){
                $night_wea=  $this->getTomorrowWea($match2[0]);
            }
            $night_tem=$this->getTem($match2[0]);
            $night_win=$this->getWin($match2[0]);
            $night_sundown=$this->getSunDown($match2[0]);
            $data_day["night_temp"]=$data["night_temp"]=$night_tem;
            $data_day["night_wea"]=$data["night_wea"]=$night_wea;
            $data_day["night_wd"]=$data["night_wd"]=isset($night_win["wd"])?$night_win["wd"]:"";
            $data_day["night_ws"]=$data["night_ws"]=isset($night_win["ws"])?$night_win["wd"]:"";
            $data_day["sun_down"]=$data["sun_down"]=strtotime(date("Y-m-d")." ".$night_sundown.":00");
        }
        if(!empty($json)){
            $data_rtime["nameen"]=$data["nameen"]=$json["nameen"];
            $data_rtime["cityname"]=$data["cityname"]=$json["cityname"];
            $data_rtime["weatherID"]=$data["weatherID"]=$json["city"];
            $data_rtime["temp"]=$data["temp"]=$json["temp"];
            $data_rtime["tempf"]=$data["tempf"]=$json["tempf"];
            $data_rtime["wd"]=$data["wd"]=$json["WD"];
            $data_rtime["wde"]=$data["wde"]=$json["wde"];
            $data_rtime["ws"]=$data["ws"]=$json["WS"];
            $data_rtime["wse"]=$data["wse"]=$json["wse"];
            $data_rtime["sd"]=$data["sd"]=$json["SD"];
            $data_rtime["rtime"]=$data["rtime"]= strtotime(date("Y-m-d")." ".$json["time"].":00");
            $data_rtime["weather"]=$data["weather"]=$json["weather"];
            $data_rtime["aqi"]=$data["aqi"]=intval($json["aqi"]);
            if($data["aqi"]<=50){
                $data_rtime["aqiname"]=$data["aqiname"]="优";
            }elseif($data["aqi"]>50&&$data["aqi"]<=100){
                $data_rtime["aqiname"]=$data["aqiname"]="良";
            }elseif($data["aqi"]>100&&$data["aqi"]<=150){
                $data_rtime["aqiname"]=$data["aqiname"]="轻度污染";
            }elseif($data["aqi"]>150&&$data["aqi"]<=200){
                $data_rtime["aqiname"]=$data["aqiname"]="中度污染";
            }elseif($data["aqi"]>200&&$data["aqi"]<=300){
                $data_rtime["aqiname"]=$data["aqiname"]="重度污染";
            }elseif($data["aqi"]>300){
                $data_rtime["aqiname"]=$data["aqiname"]="严重污染";
            }
        }
        $data_all["all"]=$data;
        $data_all["rtime"]=$data_rtime;
        $data_all["day"]=$data_day;
        return $data;
       
    }
    //获得白天和夜间的天气情况
    private function getWea($html){
        
        $match=array();
        preg_match("/(?<=<p class=\"wea\" title=\").*?(?=\")/is", $html,$match);
        if(isset($match[0]))
        {
            return trim($match[0]);
        }
        return FALSE;
    }
    //获得白天和夜间的温度
    private function getTem($html){
        $match=array();
        preg_match("/(?<=<p class=\"tem\"><span>).*?(?=<\/span>)/is", $html,$match);
        if($match[0])
        {
            return trim($match[0]);
        }
        return "";
    }
    //获得白天和夜间的风力和风向
    private function getWin($html){
        $html=preg_replace("/<i class=\".*?\"><\/i>/is", "", $html);
        $Win=array();
        $match=array();
        preg_match("/(?<=<p class=\"win\"><span class=\").*?(?=<\/span>)/is", $html,$match);
        if($match[0])
        {
            $match2=array();
            $match3=array();
            preg_match("/(?<=title=\").*?(?=\">)/is", $match[0],$match2);
            preg_match("/(?<=\">).*/is", $match[0],$match3);
            if(isset($match2[0])){
                $Win["wd"]=  trim($match2[0]);
            }
            if(isset($match3[0])){
                $Win["ws"]=  trim($match3[0]);
            }
            return $Win;
        }
        return NULL;
    }
    
    //获得日出时间
    private function getSunUp($html){
        $match=array();
        preg_match("/(?<=<p class=\"sun sunUp\"><i><\/i><span>).*?(?=<\/span><\/p>)/is", $html,$match);
        if(isset($match[0]))
        {
            return trim(str_replace("日出", "", $match[0]));
        }
        return FALSE;
    }
    //获得日落时间
    private function getSunDown($html){
        $match=array();
        preg_match("/(?<=<p class=\"sun sunDown\"><i><\/i><span>).*?(?=<\/span><\/p>)/is", $html,$match);
        if(isset($match[0]))
        {
            return trim(str_replace("日落", "", $match[0]));
        }
        return FALSE;
    }
    
    
    //获得第二天的天气信息
    public function getTomorrowWeather($code){
        $url="http://www.weather.com.cn/weather/{$code}.shtml?_=".time();
        $html=  Common::Curl_Get($url,"http://www.weather.com.cn/");
        $html=  str_replace("\r\n", "", $html);
        $html=  str_replace("\r", "", $html);
        $html=  str_replace("\n", "", $html);
        $match=array();
        //echo $html;
        preg_match("/(?<=<ul class=\"t clearfix\">).*?明天.*?(?=<\/li><li>)/is", $html,$match);
        if(isset($match[0])){
            $match[0]=preg_replace("/<li class='on'><h1>.*?<\/li><li>/", "", $match[0]);
           
        }else{
            return FALSE;
        }
        $arr=array();
        if(isset($match[0])){
            $tmp1=$this->getTomorrowT1($match[0]);
            $tmp2=$this->getTomorrowT2($match[0]);
            $wea=  $this->getTomorrowWea($match[0]);
            $wea2=  $this->getWea($match[0]);
            $arr["day_temp"]=$tmp1;
            $arr["night_temp"]=$tmp2;
            if($wea){
                $arr["weather"]=$wea;
            }elseif($wea2){
                $arr["weather"]=$wea2;
            }
            $arr["weatherID"]=$code;
            return $arr;
        }
        return FALSE;
    }
    
    //获得第二天白天气温
    public function getTomorrowT1($html){
        $match=array();
        preg_match("/(?<=<p class=\"tem\"><span>).*?(?=<\/span>)/is", $html,$match);
        if(isset($match[0])){
            return intval($match[0]);
        }
        return FALSE;
    }
    //获得第二天夜间气温
    public function getTomorrowT2($html){
        $match=array();
        preg_match("/(?<=<\/span>\/<i>).*?(?=<\/i><\/p>)/is", $html,$match);
        if(isset($match[0])){
            return intval($match[0]);
        }
        return FALSE;
    }
    
    //获得第二天夜间实时天气
    public function getTomorrowWea($html){
        $match=array();
        $html=preg_replace("/title=\".*?\"./", "", $html);
        preg_match("/(?<=<p class=\"wea\">).*?(?=<\/p>)/is", $html,$match);
        if(isset($match[0])){
            return $match[0];
        }
        return FALSE;
    }
    
}
